<?php

namespace App\Services\Draw\PoolGroupObject;

use App\Exceptions\BasicException;
use App\Jobs\QiPao\Draw\PetDrawFullGiftJob;
use Illuminate\Support\Collection;

/**
 *
 * @author wrh
 */
class PetPublicPoolGroup extends PublicPoolGroup
{

    public function getPoolPrizeList($user)
    {
        return new Collection();
    }

    public function checkDrawRoundPrizeCount(Collection $poolPrizeList, int $drawCount, $user, $poolConfig)
    {

        $templatePrizeCount = $this->poolTemplateModel->sumTemplateCount(['group_id' => $this->group_id]);
        $prizesLength       = $this->redisObject->llen($this->cachePrefix . "prizeList_" . $this->tag);
        //获取模板礼物总数 2023.12.10由 /2 改为/10  百分之50 改为百分之10
        if ($templatePrizeCount / 10 + $drawCount > $prizesLength) {
            return false;
        }
        return true;
    }

    public function createNextRoundPrizes(Collection $templatePrizes, $user, int $drawCount)
    {
        if (!$this->redisObject->set($this->cachePrefix . "fullPollLock_" . $this->tag, 1, 'ex', $this->gameLockTime, 'nx')) {
            return null;
        }
        try {
            $prizesLength = $this->redisObject->llen($this->cachePrefix . "prizeList_" . $this->tag);
            //判断奖池剩余数量 如果这次抽奖数量都不足够则同步补充礼物
            if (false && $drawCount < $prizesLength) {

                $poolGroupInfo = ['group_id' => $this->group_id, 'pool_id' => $this->pool_id, 'tag' => $this->tag];
                dispatch(new PetDrawFullGiftJob($templatePrizes, $poolGroupInfo, $this->cachePrefix))->onQueue('isGamePush');
            } else {
                if (!$this->createNextRoundPrizesWeight($templatePrizes)) {
                    return [];
                }
            }
            //补充补池金额
            $this->addPoolRepo->checkPool($this->tag, 2, $this->group_id);
        } catch (\Exception $e) {
            throw  $e;
        }
        // 清空本轮礼物总数
//        $this->poolPrizeModel->updateData(['group_id' => $this->group_id], ['template_count' => 0]);
//        $this->poolPrizeModel->appendPrizes($this, $templatePrizes);
    }

    public function decPoolPrize(array $drawPrizes)
    {
        $this->poolPrizeModel->batchDecPrizesByGroup($this, $drawPrizes);
        return true;
    }

}
